using hlib.IO;
using QntPlatform.Strategy;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace QntPlatform.Strategy.CmdCtrl
{
    public class TrendSet : StrategyPart, ICmdCtrl
    {
        IDictionary<string, int> trendDic = new Dictionary<string, int>();

        public int GetTrend(int period)
        {
            if (trendDic.TryGetValue($"{Parent.Exchange.Currency}_{period}", out var t))
            {
                return t;
            }
            return 0;
        }
        public string SetTrend(int period,int trend)
        {
            var key = Exchange.Currency +"_"+ period;
            trendDic[key] = trend;
            return key;
        }

        public static TrendSet GetContextTrendSet(int period,IDictionary<string,object> robotContext)
        {

                if (robotContext.TryGetValue("__trend", out var trend))
                {
                    return trend as TrendSet;
                }
            return null;

        }

        ILog log = Log.Default();
        public override void Init()
        {
            if (SysFun.GetRobotContext().TryGetValue("__trend", out var trend))
            {
                log.Warn("RobotContext���Ѵ���ʵ��", trend);
                throw new ArgumentException();
            }
            SysFun.GetRobotContext().Add("__trend", this);
        }
        public bool Execute(CmdContext context = null)
        {
            var str = context.Cmd.Value;
            var tNum = SysFun.ParseTo<TrandInfo>(str);
            SetTrend(tNum.Period, tNum.Trend);
            return true;
        }

 
        public TrendSet(StrategyBase parent) : base(parent)
        {
            Init();
        }

       public class TrandInfo
        {
            public int Period { get; set; }
            public int Trend { get; set; }
            //public string Group { get; set; }
        }
    }
    }